function toKeyValPair(names, values)
{
	var result = {};
	for(var i = 0; i < names.length; i++)
	{
		result[names[i]] = values[i];
	}
	return result;
}

function decileTable()
{
	var scale = 4; //MAY BE (0 TO 4) OR (1 TO 5)
	
	var pairs = [];
	$.ajax(
	{
		url: '/misc/weber/CSEvals/MyReports.cfm',
		type: "GET",
		dataType: "json",
		async: false,
		success:function(data)
		{
			var dataArray;
			var tableObject;
			$.each(data.DATA, function(i, array)
			{
				dataArray = toKeyValPair(data.COLUMNS, array);	//CONVERTS DATA TO A KEY VALUE PAIR FOR READABILITY
				if(pairs[dataArray["SEMESTER"]  + ' ' + dataArray["YEAR"]] == null) //This section puts each elemnt into an associative array based on semester and year.
					pairs[dataArray["SEMESTER"] + ' ' + dataArray["YEAR"]] = [];
				tableObject = {course:dataArray["LINK"], shareAccount:dataArray["SHAREWITHSTUDENTS"], shareDeans:dataArray["SHAREWITHCHAIRSDEANS"]
				, shareDeansDisabled:dataArray["SHAREDEANSDISABLED"], totalRespondants:dataArray["STUDENTS"], meanScore:parseFloat(dataArray["SCORE"]).toFixed(2)
				, stdDev:parseFloat(dataArray["STANDARDDEVIATION"]).toFixed(2), CRN:dataArray["BANNERCRN"], semester:dataArray["DBSEMESTER"], year:dataArray["DBYEAR"]};
				pairs[dataArray["SEMESTER"] + ' ' + dataArray["YEAR"]].push(tableObject);
			});
		}
	});
	//TEMPORARY DATA FOR TESTING
	// var tableObject = {course:"CS 2420 ONL Spring 14 32962", shareAccount:1, shareDeans:1, shareDeansDisabled:1, totalRespondants:34, meanScore:3.1, stdDev:1.25};
	// var tableObject1 = {course:"CS 2420 ONL Spring 14 32962", shareAccount:1, shareDeans:1, shareDeansDisabled:1, totalRespondants:34, meanScore:3.1, stdDev:1.25};
	// var tableObject2 = {course:"CS 2420 ONL Spring 14 32962", shareAccount:1, shareDeans:1, shareDeansDisabled:1, totalRespondants:34, meanScore:3.1, stdDev:1.25};
	// var tableObject3 = {course:"CS 2420 ONL Spring 14 32962", shareAccount:1, shareDeans:1, shareDeansDisabled:1, totalRespondants:34, meanScore:3.1, stdDev:1.25};
	// var tableObject4 = {course:"CS 2420 ONL Spring 14 32962", shareAccount:1, shareDeans:1, shareDeansDisabled:0, totalRespondants:34, meanScore:3.1, stdDev:1.25};
	// var tableObject5 = {course:"CS 2420 ONL Spring 14 32962", shareAccount:1, shareDeans:1, shareDeansDisabled:1, totalRespondants:34, meanScore:3.1, stdDev:1.25};
	// var tableObject6 = {course:"CS 2420 ONL Spring 14 32962", shareAccount:1, shareDeans:1, shareDeansDisabled:0, totalRespondants:34, meanScore:3.1, stdDev:1.25};
	// var tableObject7 = {course:"CS 2420 ONL Spring 14 32962", shareAccount:1, shareDeans:1, shareDeansDisabled:0, totalRespondants:34, meanScore:3.1, stdDev:1.25};
	// var tableObject8 = {course:"CS 2420 ONL Spring 14 32962", shareAccount:1, shareDeans:1, shareDeansDisabled:0, totalRespondants:34, meanScore:3.1, stdDev:1.25};
	// var tableObject9 = {course:"CS 2420 ONL Spring 14 32962", shareAccount:1, shareDeans:1, shareDeansDisabled:1, totalRespondants:34, meanScore:3.1, stdDev:1.25};
	// //var tableObject2 = {percentile:7, course:"1010", instructor:"Hilton, Spencer", score:3.26};
	// //var tableObject3 = {percentile:5, course:"1410", instructor:"Jensen, Joshua", score:3.05};
	// //var tableObject4 = {percentile:5, course:"2350", instructor:"Jensen, Joshua", score:3.02};
	// //var tableObject5 = {percentile:2, course:"3740", instructor:"Peterson, Brad", score:2.63};
	// //var tableObject6 = {percentile:2, course:"4750", instructor:"Jensen, Joshua", score:2.59};
	// //var tableObject7 = {percentile:1, course:"3250", instructor:"Hilton, Spencer", score:2.34};
	// //var tableObject8 = {percentile:1, course:"3750", instructor:"Hilton, Spencer", score:2.24};
	// //var tableObject9 = {percentile:1, course:"1400", instructor:"Peterson, Brad", score:2.01};
	
	// var tableObjectArray = [tableObject, tableObject1, tableObject2, tableObject3, tableObject4, tableObject5, tableObject6, tableObject7, tableObject8, tableObject9];
	//tableObjectArray[0] = tableObject;
	
	
	var rankTable =	'<table>' + 
		
		//TOP ROW LABELS
		'<tr><th>Course</th><th>Share with Wildcat Account Holder</th><th>Share with Chairs and Deans</th><th>Total Number of Respondants</th><th>Mean Score</th><th>Mean Graph</th><th>Standard Deviation</th></tr>';
		
	//TEMP
	var i = 0;
	
	for (var j in pairs)
	{
		var tableObjectArray = pairs[j];
		rankTable += '<tr>' + '<td id="semYearHead">' + j + '</td>' + '<td></td><td></td><td></td><td></td><td></td><td></td></tr>';
		for (var i = 0; i < tableObjectArray.length; i++)
		{
			rankTable += '<tr>';
			
			rankTable += '<td class="leftAlign" width = "30%"><a href="/misc/weber/CSEvals/Class_Breakdown.html?CRN=' + tableObjectArray[i]["CRN"] + '&Semester=' + tableObjectArray[i]["semester"] + '&Year=' + tableObjectArray[i]["year"] + '">' + tableObjectArray[i]["course"] + '</a></td>';
			
			rankTable += '<td>';
			
			
			if (tableObjectArray[i]["shareAccount"])
			{
				rankTable += '<input type="checkbox" id="myCheckA' + i + '" checked>'
			}
			else
			{
				rankTable += '<input type="checkbox" id="myCheckA' + i + '">'
			}
			
					
			'</td>';
			
			rankTable += '<td>';  
			
			if (tableObjectArray[i]["shareDeans"])
			{
				rankTable += '<input type="checkbox" id="myCheck' + i + '" checked>' 
			}
			else
			{
				rankTable += '<input type="checkbox" id="myCheck' + i + '">'
			}

			if (tableObjectArray[i]["shareDeansDisabled"])
			{
				rankTable += '<script>' + 
				'var check = document.getElementById("myCheck' + i +'");' +
				'check.disabled = true;' +
				'</script>';
			}

			
			rankTable += '</td>';
			
			rankTable += '<td>' + tableObjectArray[i]["totalRespondants"] + '</td>';
			
			rankTable += '<td>' + tableObjectArray[i]["meanScore"] + '</td>';
			
			rankTable += '<td><div style="position:relative">' +  
			
			
				'<img src=images/colorTopBarBackground.png style="position:absolute; left:0; z-index:0" height = "25px" width ="100%">' +
				'<img src=images/colorMean.png style="position:absolute; left:0; z-index:0" height = "25px" width = "' + ((tableObjectArray[i]["meanScore"] / scale) * 100) + '%">' +
				'<img src=images/ticksZeroToFour.png style="position:relative; z-index:1" height = "25px" width="200px">' +
			
				'</div></td>';
			
			rankTable += '<td>' + tableObjectArray[i]["stdDev"] + '</td>';
			
		}
	}
	rankTable += '</table>';
	    
	document.write(rankTable);

}

/*window.onload=(function() 
{
	decileTable();
});*/